<?php

namespace Home\Model;

use Think\Model;

class TougaoModel extends Model {

	/**
	 * 保存数据:当$id为空时添加数据;不为空则更新数据
	 *
	 * @param array $data
	 * @param string $id
	 */
	public function saveData($data, $id = '') {
		if (! get_cookie ( 'uid' )) {
			die ( '请登录后再操作!<a href="/">返回首页</a>' );
		}
		$id = intval ( $id );
		$this->error = false;

		if (! eregi ( "^[a-z]+[a-z0-9]+$", $data ['url'] )) {
			$this->error = 'url必须是字母开头,后面只能跟字母和数字';
		} elseif (! $data ['title']) {
			$this->error = '标题不能空';
		} elseif (! $data ['url']) {
			$this->error = 'url不能空';
		} elseif (! $data ['cid']) {
			$this->error = '分类不能空';
		} elseif ($this->checkUrl ( $data ['url'], $id )) {
			$this->error = 'url已经存在';
		}

		if ($id) {
			// 编辑
			if ($data ['title']) {
				$_data ['title'] = $data ['title'];
			}
			if ($data ['url']) {
				$_data ['url'] = $data ['url'];
			}
			if ($data ['cid'] != '') {
				$_data ['cid'] = $data ['cid'];
			}
			if ($data ['tag'] != '') {
				$tags = make_tags ( $data ['tag'] );
				$_data ['tag'] = $tags ['str'];
			}
			if ($data ['context'] != '') {
				$_data ['context'] = strip_tags ( preg_replace ( '/\s+style\=\"[^\"]*"/i', '', $data ['context'] ), '<p>,<strong>,<img>' );
				$_data ['context'] = preg_replace ( '/\s+(?:height|width)=\d+\b/', '', $_data ['context'] );
			}
			if ($data ['thumb'] != '') {
				$_data ['thumb'] = $data ['thumb'];
			}
			$_data ['updatetime'] = date ( "Y-m-d H:i:s" );
			if ($this->error) {
				$result ['status'] = 0;
				$result ['info'] = $this->error;
			} else {
				$_data ['status'] = - 1; // 待审核状态
				$rs = $this->where ( 'id=' . $id . ' and uid=' . intval ( get_cookie ( 'uid' ) ) )->save ( $_data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['info'] = '保存成功';
				} else {
					$result ['status'] = 0;
					$result ['info'] = '没有更新数据';
				}
			}
		} else {
			if ($this->error) {
				$result ['status'] = 0;
				$result ['info'] = $this->error;
			} else {
				$_data ['title'] = $data ['title'];
				$_data ['url'] = $data ['url'];
				$_data ['cid'] = $data ['cid'];
				$_data ['thumb'] = $data ['thumb'];
				$tags = make_tags ( $data ['tag'] );
				$_data ['tag'] = $tags ['str'];
				$_data ['updatetime'] = $data ['createtime'] = date ( "Y-m-d H:i:s" );
				$_data ['context'] = strip_tags ( preg_replace ( '/\s+style\=\"[^\"]*"/i', '', $data ['context'] ), '<p>,<strong>,<img>' );
				$_data ['context'] = preg_replace ( '/\s+(?:height|width)=\d+\b/', '', $_data ['context'] );
				$_data ['uid'] = get_cookie ( 'uid' );
				$rs = $this->add ( $_data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['info'] = '添加成功';
					$result ['id'] = $rs;
				} else {
					$result ['status'] = 0;
					$result ['info'] = '添加失败';
				}
			}
		}
		return $result;
	}

	/**
	 * 获取单条信息
	 *
	 * @param unknown $id
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, unknown, mixed, string>
	 */
	public function getInfo($id) {
		if (intval ( $id )) {
			$where = 'id=' . intval ( $id );
		} else {
			$where = 'url="' . $id . '"';
		}
		$result = $this->where ( $where )->find ();
		if ($result ['thumb']) {
			$result ['thumb500'] = C ( 'img_server' ) . str_replace ( '.', '_500_500.', $result ['thumb'] );
			$result ['thumb150'] = C ( 'img_server' ) . str_replace ( '.', '_150_150.', $result ['thumb'] );
		}
		return $result;
	}
	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = 12) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		return $result;
	}

	/**
	 * 检查url是否存在
	 *
	 * @param unknown $url
	 * @param string $id
	 *        	传入自己id可以不验证
	 * @return unknown
	 */
	public function checkUrl($url, $id = '') {
		$where = 'url="' . $url . '"';
		if ($id) {
			$where .= ' and id!=' . intval ( $id );
		}
		$rs = M ( 'article' )->where ( $where )->count ();
		if (! $rs) {
			$rs = $this->where ( $where )->count ();
		}
		return $rs;
	}

	/**
	 * 删除操作(non-PHPdoc)
	 *
	 * @see \Think\Model::delete()
	 */
	public function delete($id) {
		$result ['msg'] = '删除失败';
		$result ['status'] = 0;
		if ($id) {
			// 检查是否有子信息

			$result ['msg'] = '删除成功';
			$result ['status'] = 1;
		} else {
			$result ['msg'] = '删除失败';
			$result ['status'] = 0;
		}
		return $result;
	}
}